<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\OpinionDB;
use Illuminate\Http\Request;

class OpinionController extends BaseController
{
    /**
     * 建议
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new OpinionDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $uid = $request->post('uid',0);
        //梳理数据
        $resDb = $this->db->getIndex($uid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5);
        }
        return $this->toAppJson(0,$resDb['data']);
    }

    /**
     * 新增
     */
    public function setAdd(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $detail = $request->post('detail',0);
        if (!$uid || !$detail) {
            return $this->toAppJson(-2);
        }
        $resDb = $this->db->setAdd($uid,$detail);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-18);
        }
        $dataArr = array(
            'opinionId' => $resDb['data']['lastId'],
            'uid' => $uid,
            'opinionDetail' => $detail,
        );
        return $this->toAppJson(0,$dataArr);
    }
}